<template>
  <div>
    <div v-if="editable" class="crud-opts">
      <el-button
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="doSelectItem">选择MO生产物料</el-button>
      <el-button
        class="filter-item"
        size="mini"
        type="danger"
        icon="el-icon-delete"
        @click="doDeleteItem">删除</el-button>
      <mo-select-list ref="moSelectList" :multi-select="true" @moSelectCallback="moSelectCallback" />
      <storage-location-select-list ref="slSelectList" :multi-select="false" :warehouse-id="warehouseId" @slSelectCallback="slSelectCallback"/>
    </div>
    <DataGridTable
      :data="prodCompleteDetails"
      :show-page="false"
      size="mini"
      max-height="800px"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column prop="moCode" label="自制MO单号"/>
      <el-table-column prop="itemCode" label="物料编号"/>
      <el-table-column label="物料名称" prop="itemName" show-overflow-tooltip/>
      <el-table-column label="入库数" width="100" prop="finishProdQty">
        <template slot="header" slot-scope="scope">
          入库数<IconHelp display-content="正数(+)表示生产入库，加库存; 负数(-)表示生产退货减库存" />
        </template>
        <template slot-scope="scope">
          <el-input-number v-model.number="scope.row.finishProdQty" :disabled="!editable" :controls="false" :precision="2" size="mini" style="width: 100%" />
        </template>
      </el-table-column>
      <el-table-column label="库位">
        <template slot-scope="scope">
          <el-input v-model="scope.row.storelocationName" :disabled="!editable" style="width: 100%;">
            <el-button v-if="editable" slot="append" icon="el-icon-plus" size="mini" title="点击选择库位" @click="doSelectSl(scope.row)" />
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="单位">
        <template slot-scope="scope">
          <template v-for="item in dicts.item_unit">
            <span v-if="scope.row.unit && scope.row.unit === item.dictCode" :key="item.id" >{{ item.dictName }}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="QC检查" prop="qcCheckFlag">
        <template slot-scope="scope">
          <el-checkbox v-model="scope.row.qcCheckFlag" :disabled="!editable" true-label="Y" false-label="N" />
        </template>
      </el-table-column>
      <el-table-column label="QC结果">
        <template slot-scope="scope">
          <template v-for="item in dicts.qc_result">
            <el-tag v-if="scope.row.qcCheckResult === item.dictCode" :key="item.id" :type="item.attribute1">{{ item.dictName }}</el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="规格" prop="itemSpec" show-overflow-tooltip />
      <el-table-column label="品牌" prop="itemBrand" show-overflow-tooltip />
      <el-table-column label="备注" width="100">
        <template slot-scope="scope">
          <el-input v-model="scope.row.rmk" :disabled="!editable" size="mini" style="width: 100%"/>
        </template>
      </el-table-column>
    </DataGridTable>
  </div>
</template>

<script>
import MoSelectList from '@/views/prod/mo/module/oemMoSelectList'
import StorageLocationSelectList from '@/views/base/warehouse/storageLocation/module/storageLocationSelectList'

export default {
  dictTypes: ['item_unit', 'qc_result'],
  components: { StorageLocationSelectList, MoSelectList },
  props: {
    prodCompleteDetails: {
      type: Array,
      required: true
    },
    editable: {
      type: Boolean,
      default: false,
      required: false
    },
    warehouseId: {
      type: String,
      default: null,
      required: false
    },
    supForm: {
      type: Object,
      default: null,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      selectedDetailRows: [],
      style: 'width: 150px'
    }
  },
  methods: {
    getMaxDetailNum() {
      let detailNum = 0
      this.prodCompleteDetails.forEach(tmprow => {
        if (tmprow.num > detailNum) {
          detailNum = tmprow.num
        }
      })
      return detailNum
    },
    handleSelectionChange(val) {
      this.selectedDetailRows = val
    },
    doSelectItem() {
      /* if (!this.supForm.form.busMainData.otherSysOwnerCpy) {
        this.$notify({
          title: '请选择供应商',
          type: 'warning',
          duration: 2500
        })
        return
      }*/
      const _thisform = this.$refs.moSelectList
      _thisform.initLoadData() // this.supForm.form.busMainData.otherSysOwnerCpy)
    },
    doDeleteItem() {
      let tmpDetails = JSON.parse(JSON.stringify(this.prodCompleteDetails))
      this.selectedDetailRows.forEach(tmprow => {
        tmpDetails = tmpDetails.filter(vo => vo.num !== tmprow.num)
      })
      this.$emit('setDetails', tmpDetails)
    },
    doCopyItem() {
      const tmpDetails = JSON.parse(JSON.stringify(this.prodCompleteDetails))
      let detailNum = this.getMaxDetailNum()
      this.selectedDetailRows.forEach(tmprow => {
        const addRow = JSON.parse(JSON.stringify(tmprow))
        detailNum++
        addRow.id = null
        addRow.detailLotNo = null
        addRow.num = detailNum
        tmpDetails.push(addRow)
      })
      this.$emit('setDetails', tmpDetails)
    },
    moSelectCallback(selectedRows, sourceObject) {
      const allKeys = []
      const tmpDetails = JSON.parse(JSON.stringify(this.prodCompleteDetails))
      tmpDetails.forEach(tmprow => {
        allKeys[tmprow.moCode] = true
      })
      let detailNum = this.getMaxDetailNum()
      selectedRows.forEach(tmprow => {
        if (!allKeys[tmprow.moCode]) {
          detailNum++
          const addRow = {}
          addRow.itemId = tmprow.itemId
          addRow.unit = tmprow.unit
          addRow.itemCode = tmprow.itemCode
          addRow.itemName = tmprow.itemName
          addRow.itemSpec = tmprow.itemSpec
          addRow.itemBrand = tmprow.itemBrand
          addRow.moCode = tmprow.moCode
          addRow.finishProdQty = tmprow.balanceQty
          addRow.id = null
          addRow.num = detailNum
          tmpDetails.push(addRow)
        }
      })
      this.$emit('setDetails', tmpDetails)
      sourceObject.cancel()
    },
    doSelectSl(row) {
      const _thisform = this.$refs.slSelectList
      _thisform.initLoadData(row)
    },
    slSelectCallback(selectedRow, sourceObject, paramData) {
      const tmpDetails = JSON.parse(JSON.stringify(this.prodCompleteDetails))
      tmpDetails.forEach(tmprow => {
        if (tmprow.num === paramData.num) {
          tmprow.storelocationId = null
          tmprow.storelocationName = null
          if (selectedRow) {
            tmprow.storelocationId = selectedRow.id
            tmprow.storelocationName = selectedRow.slName
          }
          return
        }
      })
      this.$emit('setDetails', tmpDetails)
      sourceObject.cancel()
    }
  }
}
</script>
